package com.example.pokemon_chat_room;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.pokemon_chat_room.adapter.MsgAdapter;
import com.example.pokemon_chat_room.adapter.PokeDexAdapter;
import com.example.pokemon_chat_room.pojo.Msg;
import com.example.pokemon_chat_room.pojo.Pokemon;
import com.example.pokemon_chat_room.request.OkHttp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class pokedex extends AppCompatActivity {

    private RecyclerView recyclerView;
    private PokeDexAdapter pokeDexAdapter;
    private List<Pokemon> pokemonList = new ArrayList<>();



    String post;

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pokedex);

        String menuTitle = "宝可梦图鉴";
        androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setCustomView(R.layout.title_layout);//设置标题样式
            TextView textView = (TextView) actionBar.getCustomView().findViewById(R.id.display_title);//获取标题布局的textview
            textView.setText(menuTitle);//设置标题名称，menuTitle为String字符串
            actionBar.setHomeButtonEnabled(true);//设置左上角的图标是否可以点击
            actionBar.setDisplayHomeAsUpEnabled(true);//给左上角图标的左边加上一个返回的图标
            actionBar.setDisplayShowCustomEnabled(true);// 使自定义的普通View能在title栏显示，即actionBar.setCustomView能起作用

        }



        OkHttp okHttp = new OkHttp();


        recyclerView = findViewById(R.id.poke_list);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new MyDecoration());

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject jsonObject = okHttp.Post(getApplicationContext(),"pokemon/getAll", null);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.size(); i++) {
                        Pokemon pokemon = (Pokemon) data.getObject(i, Pokemon.class);
                        pokemonList.add(pokemon);
                    }
                    pokedex.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            pokeDexAdapter = new PokeDexAdapter(pokedex.this, pokemonList);
                            recyclerView.setAdapter(pokeDexAdapter);
                            recyclerView.scrollToPosition(0);
                        }
                    });

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

    }

     class MyDecoration extends RecyclerView.ItemDecoration{
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            //outRect.set()中的参数分别对应左、上、右、下的间隔
            outRect.set(10,10,10,10);
        }
    }
}